<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/9
 * Time: 14:03
 */
namespace work{

    use app\trip;
    use C\ctr;
    use C\lib\Db;
    use C\lib\Session;
    use C\mod\uaddres;
    use C\lib\Text;
    use C\lib\Ditu;
    use C\mod\order;
    use C\mod\worker;
    use C\lib\Jpush;
    use C\mod\workerd;
    use C\mod\wprice;
    use C\lib\Wxapi;
    use C\mod\tui;

    class bexpress extends ctr{

        // --- 定义构造函数 ---
        function __construct() {
            Db::connect();
        }
        /*
         * 快递专区页面
         */
        public function index(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                $this->loadView("bdkd/express/index");
            }
        }
        /*
         * 去快递页面
         */
        public function qu(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){

                $a=[
                    'signPackage'=>Wxapi::getSignPackage()
                ];

                $this->loadView("bdkd/express/qukuaidi",['list'=>$a]);
            }
        }
        /*
         * 物流托运页面
         */
        public function wu(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                $this->loadView("bdkd/express/wu");
            }
        }
        /*
        * 查询价格页面
        */
        public function price(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                $this->loadView("bdkd/express/price");
            }

        }
        /*
         * 查询时间页面
         */
        public function time(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                $this->loadView("bdkd/express/time");
            }

        }
        /*
         * 选择重量页面
         */
        public function weight(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){
                //var_dump($_GET);
                $num=$_GET['num'];
                $this->loadView("bdkd/express/weight",['num'=>$num]);
            }
        }

        /*
         * 寄快递页面
         */
        public function ji(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){

//                $num=$_GET['num'];

                $this->loadView('bdkd/express/index');
                //$this->loadView('bdkd/express/index');
            }
        }

        public function wl(){

            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){
                $num=$_GET['num'];
                $this->loadView('bdkd/express/wu',['num'=>$num]);
                //$this->loadView('bdkd/express/wu');
            }
        }

        public function qk(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){
                $num=$_GET['num'];
                $a=[
                    'signPackage'=>Wxapi::getSignPackage()
                ];
                $this->loadView('bdkd/express/qukuaidi',['num'=>$num,'list'=>$a]);
            }
        }

        /*
         * 寄快递地址信息页面
         * state:1从发件人点进来不显示地址信息 2从收件人点进来显示地址信息
         *num:
         */
        public function sendinfo(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){

                $list= [];
                $data=uaddres::getList(['user_id'=>$_SESSION['user.id'],'type'=>1]);
                //print_r($data);
                foreach ($data as $value){
                    $list[]=[
                        'id'=>$value->id,
                        'name'=>$value->name,
                        'phone'=>$value->phone,
                        'addres'=>$value->addres,
                        'type'=>$value->type
                    ];
                }

                $this->loadView('bdkd/express/dizhiji',['list'=>$list]);
            }
        }

        /*
         * 从收件人地址
         */
        public function getinfo(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){
                $this->loadView("bdkd/express/dizhis");
            }
        }

        /*
         * 取快递地址信息页面
         */
        public function getGood(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){

                $list= [];
                $data=uaddres::getList(['user_id'=>$_SESSION['user.id'],'type'=>1]);
                //print_r($data);
                foreach ($data as $value){
                    $list[]=[
                        'id'=>$value->id,
                        'name'=>$value->name,
                        'phone'=>$value->phone,
                        'addres'=>$value->addres,
                        'type'=>$value->type
                    ];
                }
               // $list=$this->getaddres();
                $this->loadView('bdkd/express/dizhi',['list'=>$list]);
            }
        }

        /*
         * 物流地址信息页面
         */
        public function sendwu(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){
                $list= [];
                $data=uaddres::getList(['user_id'=>$_SESSION['user.id'],'type'=>1]);
                //print_r($data);
                foreach ($data as $value){
                    $list[]=[
                        'id'=>$value->id,
                        'name'=>$value->name,
                        'phone'=>$value->phone,
                        'addres'=>$value->addres,
                        'type'=>$value->type
                    ];
                }

                $this->loadView('bdkd/express/dizhiwu',['list'=>$list]);
            }
        }
        /*
         * 删除地址信息
         */
        public function deladdres(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                if(empty($_POST['id'])){
                    $this->writeJson(0,'请选择要删除的信息!');exit;
                }
                $uaddres=uaddres::delete(['id'=>$_POST['id']]);
                if($uaddres){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'删除失败!');
                }
            }
        }

        /*
         * 从收件人地址
         */
        public function getwu(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){
                $this->loadView("bdkd/express/dizhiswu");
            }
        }


        /*
          * 快递地址信息添加
          */
        public function addres(){
            Db::connect();
            Session::start();

            if(isset($_SESSION['user.id'])){

               // var_dump($_POST);

                if(!empty($_POST['location'])){
                }else{
                    $this->writeJson(0,'请输入地址!');exit;
                }
                if(!empty($_POST['type'])){
                }else{
                    $this->writeJson(0,'缺少type参数!');exit;
                }

                if(!empty($_POST['address'])){

                    if(!Ditu::curlGetWeb(str_replace(" ",'',$_POST['location'].$_POST['address']))){
                        $this->writeJson(0,'请填写正确的地址!');exit;
                    }
                }else{
                    $this->writeJson(0,'请填写详细地址!');exit;
                }
                if(!empty($_POST['phone'])){
                    if(!Text::isPhone($_POST['phone'])){
                        $this->writeJson(0,'手机号格式不正确');exit;
                    }
                }else{
                    $this->writeJson(0,'请输入手机号!');exit;
                }
                if(!empty($_POST['uname'])){
                }else{
                    $this->writeJson(0,'请输入姓名!');exit;
                }
                $address=Ditu::curlGetWeb(str_replace(" ",'',$_POST['location'].$_POST['address']));
               // var_dump($address);
               // $res=Ditu::bdTOgcj($address['lat'],$address['lng']);


                $addres=new uaddres();
                $addres->set([
                    'user_id'=>$_SESSION['user.id'],
                    'addres' => str_replace(" ",'',$_POST['location'].$_POST['address']),
                    'phone' => trim($_POST['phone']),
                    'name' => trim($_POST['uname']),
                    'lat' => $address['lat'],
                    'lng' => $address['lng'],
                    'type' => $_POST['type']
                ]);
                // var_dump($address);//exit;

                if($addres->create()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'提交失败!');
                }
//
//                $data=uaddres::get(['user_id'=>$_SESSION['user.id'],'lat'=>$address['lat'],'lng'=>$address['lng']]);
//                if($data){
//                    $this->writeJson(1);
//                }else{
//
//                }
            }else
                $this->loadView('bdkd/sign/main');

        }
        /*
         * 显示用户地址信息
         */
        public function getaddres(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                $list= [];
                $data=uaddres::getList(['user_id'=>$_SESSION['user.id']]);
                foreach ($data as $value){
                    $list[]=[
                        'name'=>$value->name,
                        'phone'=>$value->phone,
                        'addres'=>$value->addres
                    ];
                }

                return $list;
            }else
                $this->loadView('work/sgin/main');//登录页面
        }
        /*
         * 显示寄件页面
         */
        public function sOrder(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                $this->loadView('bdkd/express/index');
            }else
                $this->loadView('work/sgin/main');//登录页面
        }
        /*

        /*
         * 寄快递
         */
        public function sendOrder(){
            Db::connect();
            Session::start();
           // $this->writeJson(0,$_POST['type']);exit;

            if(isset($_SESSION['user.id'])){

                $checkDayStr = date('Y-m-d ',time());
                $timeBegin1 = strtotime($checkDayStr."09:00".":00");
                $timeEnd1 = strtotime($checkDayStr."19:00".":00");

                $curr_time = $_SERVER['REQUEST_TIME'];
                if($curr_time >= $timeBegin1 && $curr_time <= $timeEnd1){
                    $data= [];
                    //var_dump($_POST);//exit;
                    if(!empty($_POST['sname'])){
                        $data['name_sender']=$_POST['sname'];
                    }else{
                        $this->writeJson(0,'请输入寄件人姓名!');exit;
                    }
                    if(!empty($_POST['sphone'])){
                        if(!Text::isPhone($_POST['sphone'])){
                            $this->writeJson(0,'请输入正确格式的手机号!');exit;
                        }else{
                            $data['phone_sender']=$_POST['sphone'];
                        }
                    }else{
                        $this->writeJson(0,'请输入寄件人手机号!');exit;
                    }
                    if(!empty($_POST['saddress'])){
                        $data['address_sender']=str_replace(" ",'',$_POST['saddress']);
                    }else{
                        $this->writeJson(0,'请输入寄件人地址!');exit;
                    }
                    //var_dump($data);exit;


                    if(!empty($_POST['gaddress'])){
                        $data['address_recipient']=str_replace(" ",'',$_POST['gaddress']);;
                    }else{
                        $this->writeJson(0,'请输入收件人地址!');exit;
                    }
                    if(!empty($_POST['gphone'])){
                        if(!Text::isPhone($_POST['gphone'])){
                            $this->writeJson(0,'请输入正确格式的手机号');exit;
                        }else{
                            $data['phone_recipient']=$_POST['gphone'];
                        }
                    }else{
                        $this->writeJson(0,'请输入收件人手机号!');exit;
                    }
                    if(!empty($_POST['gname'])){
                        $data['name_recipient']=$_POST['gname'];
                    }else{
                        $this->writeJson(0,'请输入收件人姓名!');exit;
                    }

                    if(!empty($_POST['weight'])){
                        $data['weight']=$_POST['weight'];
                    }else{
                        $this->writeJson(0,'请选择物品重量!');exit;
                    }
                    if(!empty($_POST['type'])){
                        $data['goods_type']=$_POST['type'];
                    }else{
                        $this->writeJson(0,'请选择物品类型!');exit;
                    }
                    if(!empty($_POST['time'])){
                        $data['time']=$_POST['time'];
                    }else{
                        $this->writeJson(0,'请选择寄件时间!');exit;
                    }
                    //选择要推送的快递公司
                    if(!empty($_POST['express_type'])){
                        $data['express_type']=$_POST['express_type'];
                    }else{
                        $this->writeJson(0,'请选择快递公司');exit;
                    }
                    if(!empty($_POST['remark'])){
                        $data['remark']=$_POST['remark'];//备注
                    }
                    $data['order_id']=order::build_order_no();
                    $data['time_add']=$_SERVER['REQUEST_TIME'];
                    $data['user_id']=$_SESSION['user.id'];
                    $data['type']=1;
                    $data['state']=0;
                    $address=Ditu::curlGetWeb($data['address_sender']);
                    //var_dump($address);
//                    $data['lat']=$address['lat'];
//                    $data['lng']=$address['lng'];
                    $data['lat']='40.024199';
                    $data['lng']='116.300884';

                    $order=new order();
                    $order->set($data);

                    if($order->create()){
                        $o_id=$order->id;
                        //$this->pushInfo(1,$data['lat'],$data['lng'],3);
                        //用户下单成功 给快递员推送消息
                        $data['lat']='40.024199';
                        $data['lng']='116.300884';
                        $push=$this->pushInfo(1,1,$data['lat'],$data['lng'],2,$_POST['express_type']);
                       // var_dump($push);
                        $this->writeJson(1);exit;
//                        if($push){
//
//                        }else{
//                            $del=order::delete(['id'=>$o_id]);
//                            $this->writeJson(0,'抱歉附近没有接单员请换个时间再来吧!');exit;
//                        }
                    }else{
                        $this->writeJson(0,'寄件失败请重新提交!2');exit;
                    }
                }else{
                    $this->writeJson(0,'快递员还在休息暂时不能下单哦~');exit;
                }
            }
        }



        /*
         * 物流托运
         */
        public function logistics(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                // var_dump($_POST);exit;
                $checkDayStr = date('Y-m-d ',time());
                $timeBegin1 = strtotime($checkDayStr."09:00".":00");
                $timeEnd1 = strtotime($checkDayStr."19:00".":00");

                $curr_time = $_SERVER['REQUEST_TIME'];
                //$curr_time >= $timeBegin1 && $curr_time <= $timeEnd1
                if($curr_time >= $timeBegin1 && $curr_time <= $timeEnd1){
                    $data= [];
                    //var_dump($_POST);exit;
                    if(!empty($_POST['sname'])){
                        $data['name_sender']=$_POST['sname'];
                    }else{
                        $this->writeJson(0,'请输入寄件人姓名!');exit;
                    }
                    if(!empty($_POST['sphone'])){
                        if(!Text::isPhone($_POST['sphone'])){
                            $this->writeJson(0,'请输入正确格式的手机号');exit;
                        }else{
                            $data['phone_sender']=$_POST['sphone'];
                        }
                    }else{
                        $this->writeJson(0,'请输入寄件人手机号!');exit;
                    }
                    if(!empty($_POST['saddress'])){
                        $data['address_sender']=str_replace(" ",'',$_POST['saddress']);
                    }else{
                        $this->writeJson(0,'请输入寄件人地址!');exit;
                    }

                    if(!empty($_POST['gaddress'])){
                        $data['address_recipient']=str_replace(" ",'',$_POST['gaddress']);;
                    }else{
                        $this->writeJson(0,'请输入收件人地址!');exit;
                    }
                    if(!empty($_POST['gphone'])){
                        if(!Text::isPhone($_POST['gphone'])){
                            $this->writeJson(0,'请输入正确格式的手机号');exit;
                        }else{
                            $data['phone_recipient']=$_POST['gphone'];
                        }
                    }else{
                        $this->writeJson(0,'请输入收件人手机号!');exit;
                    }
                    if(!empty($_POST['gname'])){
                        $data['name_recipient']=$_POST['gname'];
                    }else{
                        $this->writeJson(0,'请输入收件人姓名!');exit;
                    }

                    if(!empty($_POST['w_type'])){
                        $data['w_type']=$_POST['w_type'];
                    }else{
                        $this->writeJson(0,'请输入物品类型!');exit;
                    }

                    if(!empty($_POST['time'])){
                        $data['time']=$_POST['time'];
                    }else{
                        $this->writeJson(0,'缺少寄件时间参数!');exit;
                    }
                    //选择要推送的快递公司
                    if(!empty($_POST['express_type'])){
                        $data['express_type']=$_POST['express_type'];
                    }else{
                        $this->writeJson(0,'请选择物流公司!');exit;
                    }
                    if(!empty($_POST['remark'])){
                        $data['remark']=$_POST['remark'];//备注
                    }
                    $data['order_id']=order::build_order_no();
                    $data['time_add']=$_SERVER['REQUEST_TIME'];
                    $data['user_id']=$_SESSION['user.id'];
                    $data['type']=2;
                    $data['state']=0;
                    $address=Ditu::curlGetWeb($data['address_sender']);
                    //var_dump($address);
//                    $data['lat']=$address['lat'];
//                    $data['lng']=$address['lng'];
                    $data['lat']='40.024199';
                    $data['lng']='116.300884';
                    // var_dump($data);exit;
                    $order=new order();
                    $order->set($data);

                    if($order->create()){
                        $o_id=$order->id;
                        //用户下单成功 给快递员推送消息
//                        $push=$this->pushInfo(2,1,$data['lat'],$data['lng'],1,$_POST['express_type']);
                       // echo 222;exit;
                        $data['lat']='40.024199';
                        $data['lng']='116.300884';
                       // var_dump($data);exit;
                        $push=$this->pushInfo(2,1,$data['lat'],$data['lng'],2,$_POST['express_type']);
                        // var_dump($push);
                        if($push){
                            $this->writeJson(1);
                        }else{
                            $del=order::delete(['id'=>$o_id]);
                            $this->writeJson(0,'抱歉，您附近暂时没有快递员!');
                        }
                    }else{
                        $this->writeJson(0,'失败请重新提交!');
                    }
                }else{
                    $this->writeJson(0,'快递员还在休息暂时不能下单哦~');
                }
            }
        }

        /*
        * 取快递
        */
        public function getOrder(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                //$this->writeJson(0,$_POST);exit;

                $checkDayStr = date('Y-m-d ',time());
                $timeBegin1 = strtotime($checkDayStr."09:00".":00");
                $timeEnd1 = strtotime($checkDayStr."19:00".":00");

                $curr_time = $_SERVER['REQUEST_TIME'];
                //$curr_time >= $timeBegin1 && $curr_time <= $timeEnd1
                if(1){
                    //$data= [];
                    //var_dump($_POST);//exit;
                    if(!empty($_POST['gname'])){
//                        $data['name_sender']=$_POST['sname'];
                    }else{
                        $this->writeJson(0,'请输入取件人姓名！');exit;
                    }
                    if(!empty($_POST['gphone'])){
                        if(!Text::isPhone($_POST['gphone'])){
                            $this->writeJson(0,'请输入正确格式的手机号');exit;
                        }
                    }else{
                        $this->writeJson(0,'请输入取件人手机号!');exit;
                    }
                    if(empty($_POST['gaddress'])){
                        $this->writeJson(0,'请输入取件人地址!');exit;
                    }
//                    if(empty($_POST['latitude'])){
//                        $this->writeJson(0,'缺少latitude!');exit;
//                    }
//                    if(empty($_POST['longitude'])){
//                        $this->writeJson(0,'缺少longitude!');exit;
//                    }

                    if(empty($_POST['remarkArr'])){
                        $this->writeJson(0,'请输入取件信息！');exit;
                    }
                    $gaddress=str_replace(" ",'',$_POST['gaddress']);


                    foreach ($_POST['remarkArr'] as $value){
                        $address=Ditu::curlGetWeb($gaddress);//高德坐标
                       // $res=Ditu::bdTOgcj($address['lat'],$address['lng']);

                        $order=new order();
                        $order->set([
                            'user_id'=>$_SESSION['user.id'],
                            'address_recipient'=>$_POST['gaddress'],
                            'phone_recipient'=>$_POST['gphone'],
                            'name_recipient'=>$_POST['gname'],
                            'order_id'=>order::build_order_no(),
                            'type'=>0,
                            'state'=>0,
                            'remark'=> $value,
//                            'lat'=>$address['lat'],
//                            'lng'=>$address['lng'],
                            'lat'=>'40.024199',
                            'lng'=>'116.300884',
                            'time_add'=>$_SERVER['REQUEST_TIME']
                        ]);
                        $order->create();
                        $data['lat']='40.024199';
                        $data['lng']='116.300884';

                        $this->pushInfo('',0,$address['lat'],$address['lng'],3,'');
                    }
                    $this->writeJson(1);exit;

                }else{
                    $this->writeJson(0,'快递员还在休息暂时不能下单哦~');
                }
            }
        }
        /*
         * 推送消息
         * ptype:1 普通快递 2物流托运？？？？？取快递要不要推送  给谁推送
         * type:1快递员 0取递员
         * wtype:消息分类推送(顺丰 三通一达 优速 百世物流 德邦物流)
         * distance:推送范围 (周围公里)
         */
        private function pushInfo($ptype,$type,$lat,$lng,$distance,$wtype){
            //满足推送条件的推送
            //查询到在燕丹区域内的快递员
            //$distance=2;
//            $lat='40.024199';
//            $lng='116.301271';
            $stype=[];
            if(!empty($ptype)){
                if($ptype==1){
                    if($wtype==1){
                        $stype=['顺丰'];
                    }
                    if($wtype==2){
                        $stype=['中通','申通','圆通','韵达'];
                    }
                    if($wtype==3){
                        $stype=['优速'];
                    }
                }
                if($ptype==2){
                    //$stype=['百世物流','德邦物流'];
                    if($wtype==5){
                        $stype=['百世物流'];
                    }
                    if($wtype==6){
                        $stype=['德邦物流'];
                    }
                }
            }
           // echo 555;//exit;
            //获取用户周围n公里内的快递员(取递员)
            //如果附近有快递员推送 没有则返回0  推送成功返回1 不成功返回0
            $list=worker::scop($lat,$lng,$distance);

           //  print_r($list);//exit;
            //var_dump($list);exit;
            if($list){
                $m_time = '600';//离线保留时间
                $tag= [];
                foreach ($list as $val){
                    if($type==0){//代取员 (1)先根据工人类型判断推送目标
                        $workerd=workerd::get(['worker_id'=>$val->id,'type'=>0]);
                        //var_dump($workerd);
                        if($workerd){
                            //查询是否缴纳押金  交押金的推送  (2)是否要根据学校名 或负责的代取位置进行推送????

                            $wprice=wprice::get(['pay_state'=>1,'worker_id'=>$workerd->worker_id]);
                           // var_dump($wprice);

                            if($wprice){
                                $tag[]=$wprice->worker_id;
                            }
                        }
                    }else{//快递物流

                        //判断  那一部分需要推送 需要推送的是否交过押金
                        $workerd=workerd::get(['worker_id'=>$val->id,['subsidiary','in',$stype]]);
                        //var_dump($workerd);//exit;
                        //var_dump($workerd->worker_id);//exit;
                        if($workerd){
                            $wprice=wprice::get(['pay_state'=>1,'worker_id'=>$workerd->worker_id]);
                            if($wprice){
                                $tag[]=$wprice->worker_id;
                            }
                        }
                    }
                }
                //var_dump($tag);//exit;
                // echo 333;

                if(!empty($tag)){
                    //var_dump($tag);exit;
                    //$content='您有一个订单是否要接单:'.$name.'电话:'.$phone.'预约寄件时间为:'.$time;
                    if($type==1){
                        $content='您有一寄快递个订单';
                    }else{
                        $content='您有一个代取快递订单';
                    }

                    $message="";//存储推送状态
                    $result = Jpush::push('all',$content,'','',$m_time,$tag);


                    $res_arr = json_decode($result, true);
                    if(isset($res_arr['error'])){
                        if($res_arr['error']['code']==200){
                            foreach($tag as $value){
                                $tui=new tui();
                                $tui->set([
                                    'wid'=>$value,
                                    'time_add'=>time()
                                ]);
                                $tui->create();
                            }
                        }
                    }else{
                        foreach($tag as $value){
                            $tui=new tui();
                            $tui->set([
                                'wid'=>$value,
                                'time_add'=>time()
                            ]);
                            $tui->create();
                        }
                    }


                    if($result){
                        $res_arr = json_decode($result, true);
                        if(isset($res_arr['error'])){                       //如果返回了error则证明失败
                            //echo $res_arr['error']['message'];          //错误信息
                            $error_code=$res_arr['error']['code'];             //错误码
                            switch ($error_code) {
                                case 200:
                                    $message= '发送成功！';
                                    // $message;
                                    return true;
                                    break;
                                case 1000:
                                    $message= '失败(系统内部错误)';
                                    // echo $message;
                                    return false;
                                    break;
                                case 1001:
                                    $message = '失败(只支持 HTTP Post 方法，不支持 Get 方法)';//echo $message;
                                    return false;
                                    break;
                                case 1002:
                                    $message= '失败(缺少了必须的参数)';//echo $message;
                                    return false;
                                    break;
                                case 1003:
                                    $message= '失败(参数值不合法)';//echo $message;
                                    return false;
                                    break;
                                case 1004:
                                    $message= '失败(验证失败)';//echo $message;
                                    return false;
                                    break;
                                case 1005:
                                    $message= '失败(消息体太大)';//echo $message;
                                    return false;
                                    break;
                                case 1008:
                                    $message= '失败(appkey参数非法)';//echo $message;
                                    return false;
                                    break;
                                case 1011:
                                    $message= '没有满足条件的推送对象';//echo $message;
                                    return false;
                                    break;
                                case 1020:
                                    $message= '失败(只支持 HTTPS 请求)';//echo $message;
                                    return false;
                                    break;
                                case 1030:
                                    $message= '失败(内部服务超时)';//echo $message;
                                    return false;
                                    break;
                                default:
                                    $message= '失败(返回其他状态，目前不清楚额，请联系开发人员！)';//echo $message;
                                    return false;
                                    break;
                            }
                        }else{
                            $message="发送成功！";//echo $message;
                            return true;
                        }
                    }else{
                        $message='接口调用失败或无响应';//echo $message;
                        return false;
                    }
                    //echo  "<script>alert('推送信息:{$message}')</script>";

                }else{
                    return false;
                }
            }else{
                return false;
            }

        }


        /*
         * 取消订单
         */
        public function  cancel(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                if(empty($_POST['id'])){
                    $this->writeJson(0,'缺少订单id!');exit;
                }
                $order=order::get(['id'=>$_POST['id']]);
                $order->set([
                    'state'=>3
                ]);
                if($order->update()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'取消订单失败请联系客服!');
                }
            }
        }
    }
}